package com.activiti.z_six.service.impl;

import com.activiti.z_six.entity.formComponents.CreateTable;
import com.activiti.z_six.entity.formComponents.CustomTable;
import com.activiti.z_six.entity.formComponents.FormEntity;
import com.activiti.z_six.entity.formComponents.FormSortEntity;
import com.activiti.z_six.entity.taskAssignee.GenerWork;
import com.activiti.z_six.mapper.formComponentsMapper.FormEntityMapper;
import com.activiti.z_six.mapper.formComponentsMapper.FormSortEntityMapper;
import com.activiti.z_six.mapper.taskAssigneeMapper.GenerWorkMapper;
import com.activiti.z_six.service.FormMapService;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class FormMapServiceImpl implements FormMapService {
    @Autowired
    private FormEntityMapper formEntityMapper;
    @Autowired
    private FormSortEntityMapper formSortEntityMapper;
    @Autowired
    private GenerWorkMapper generWorkMapper;

    /**
     * 获取表单列表
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public HashMap<String,Object> formList(String name,String formSort,Integer page,Integer pagesize){
        HashMap<String,Object> hashMap=new HashMap<>();
        try{
            List<FormEntity> formList = formEntityMapper.formList(name,formSort);
            if(pagesize==0) {
                hashMap.put("list",formList);
                hashMap.put("total",formList.size());
            }
            else{
                Integer startIndex=0;
                if(page>1){
                    startIndex=(page-1)*pagesize;
                }
                List<FormEntity> formListPage=formEntityMapper.formListPage(name,formSort,startIndex,pagesize);
                hashMap.put("list",formListPage);
                hashMap.put("total",formList.size());
            }
            return hashMap;
        }
        catch (Exception ex){
            return hashMap;
        }
    }
    @Override
    public FormEntity getFormEntity(String id){
        try{
            FormEntity formEntity=formEntityMapper.formEntity(id);
            return formEntity;
        }
        catch (Exception ex){
            return new FormEntity();
        }
    }
    @Override
    public String addForm(String id,String name,String createTime,String formSort,String form_event
            ,String form_event_link,String form_data_table,String form_sub_tables,String form_json){
        try{
            FormEntity formEntity=new FormEntity();
            formEntity.setId(id);
            formEntity.setName(name);
            formEntity.setCreateTime(createTime);
            formEntity.setFormSort(formSort);
            formEntity.setForm_event(form_event);
            formEntity.setForm_event_link(form_event_link);
            formEntity.setForm_data_table(form_data_table);
            formEntity.setForm_sub_tables(form_sub_tables);
            formEntity.setForm_json(form_json);

            if(!SystemConfig.IsNullOrEmpty(form_json)) {
                JSONObject jsonObject=JSONObject.parseObject(form_json);
                String msg=createTableByJson(form_data_table,jsonObject.getString("list"),false);
                if(!msg.equals("suess")){
                    return msg;
                }
            }
            //增加表单模版
            formEntityMapper.addForm(formEntity);
            return "执行成功";
        }
        catch (Exception ex){
            return ex.getMessage().toString();
        }
    }
    @Override
    public String deleteForm(String id){
        try{
            formEntityMapper.deleteForm(id);
            return "执行成功";
        }
        catch (Exception ex){
            return ex.getMessage().toString();
        }
    }
    @Override
    public String updateForm(String id,String name,String createTime,String formSort,String form_event
            ,String form_event_link,String form_data_table,String form_sub_tables,String form_json){
        try{
            FormEntity formEntity=new FormEntity();
            formEntity.setId(id);
            formEntity.setName(name);
            formEntity.setCreateTime(createTime);
            formEntity.setFormSort(formSort);
            formEntity.setForm_event(form_event);
            formEntity.setForm_event_link(form_event_link);
            formEntity.setForm_data_table(form_data_table);
            formEntity.setForm_sub_tables(form_sub_tables);
            formEntity.setForm_json(form_json);
            //更新模版
            formEntityMapper.updateForm(formEntity);

            //判断是否已经存在该表
            int isTableExist=formEntityMapper.isTableExist(form_data_table);
            if(isTableExist>0) {
                AlterTableAddField(form_data_table,form_json);
            }
            else{
                JSONObject jsonObject=JSONObject.parseObject(form_json);
                String msg=createTableByJson(form_data_table,jsonObject.getString("list"),false);
                if(!msg.equals("suess")){
                    return msg;
                }
            }
            return "执行成功";
        }
        catch (Exception ex){
            return ex.getMessage().toString();
        }
    }
    @Override
    public List<FormSortEntity> formSortList(){
        try{
            List<FormSortEntity> formSortList=formSortEntityMapper.formSortList();
            //2、组装树形结构
            List<FormSortEntity> newList = formSortList.stream()
                    .filter(t -> t.getParentNo().equals("0"))
                    .map((menu) -> {
                        menu.setChildren(this.getChildren(menu,formSortList));
                        return menu;
                    })
                    .collect(Collectors.toList());
            return newList;
        }
        catch (Exception ex){
            return new ArrayList<>();
        }
    }
    @Override
    public FormSortEntity getFormSort(String id){
        try{
            FormSortEntity formSortEntity=formSortEntityMapper.formSort(id);
            return formSortEntity;
        }
        catch (Exception ex){
            return new FormSortEntity();
        }
    }
    @Override
    public String addFormSort(String id,String name,String parentNo){
        try{
            FormSortEntity formSortEntity=new FormSortEntity();
            formSortEntity.setId(id);
            formSortEntity.setName(name);
            formSortEntity.setParentNo(parentNo);
            formSortEntityMapper.addFormSort(formSortEntity);
            return "执行成功";
        }
        catch (Exception ex){
            return ex.getMessage().toString();
        }
    }
    @Override
    public String deleteFormSort(String id){
        try{
            formSortEntityMapper.deleteFormSort(id);
            return "执行成功";
        }
        catch (Exception ex){
            return ex.getMessage().toString();
        }
    }
    @Override
    public String updateFormSort(String id,String name,String parentNo){
        try{
            FormSortEntity formSortEntity=new FormSortEntity();
            formSortEntity.setId(id);
            formSortEntity.setName(name);
            formSortEntity.setParentNo(parentNo);
            formSortEntityMapper.updateFormSort(formSortEntity);
            return "执行成功";
        }
        catch (Exception ex){
            return ex.getMessage().toString();
        }
    }
    /**
     * 递归查找当前菜单的子菜单
     * @param root 单个对象
     * @param all 所有的集合
     * @return 排序后的子类
     */
    private List<FormSortEntity> getChildren(FormSortEntity root,List<FormSortEntity> all){
        List<FormSortEntity> childrenList = all.stream()
                .filter(t -> t.getParentNo().equals(root.getId()) )
                .map(g -> {
                    //找子菜单
                    g.setChildren(getChildren(g,all));
                    return g;
                })
                //菜单排序
                .collect(Collectors.toList());

        return childrenList;
    }
    /*
    根据json动态创建数据库表
     */
    private String createTableByJson(String tableName,String columnsJson,Boolean isSub){
        String msg="";
        //要创建的表的集合
        List<CreateTable> createTables=new ArrayList<>();
        JSONArray listJson=JSONArray.parseArray(columnsJson);

        int isTableExist=formEntityMapper.isTableExist(tableName);
        if(isTableExist>0)
            return "该表["+tableName+"]已经存在，请重新命名表名。";

        try {
            //创建表对象
            CreateTable createTable = new CreateTable();
            createTable.setTableName(tableName);
            //列对象集合
            List<CustomTable> customTableList = new ArrayList<>();

            //列对象
            CustomTable customTable = new CustomTable();
            if (isSub) {

                customTable.setChoose(true);
                customTable.setCreateTableFiledName("fk_id");
                customTable.setComment("主表id");
                customTable.setFieldType("varchar(255)");
                customTableList.add(customTable);

            }
            for (int i = 0; i < listJson.size(); i++) {
                customTable = new CustomTable();
                JSONObject list = listJson.getJSONObject(i);
                //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                if (list.getString("compType").equals("text")
                        || list.getString("compType").equals("barCode")
                        || list.getString("compType").equals("alert")
                        || list.getString("compType").equals("link")
                        || list.getString("compType").equals("button")
                        || list.getString("compType").equals("colorPicker")
                        || list.getString("compType").equals("divider")
                        || list.getString("compType").equals("upload"))
                    continue;
                if (list.getString("compType").equals("dynamicTable")) {
                    if (!SystemConfig.IsNullOrEmpty(list.getString("columns"))) {
                        JSONArray childrens = JSONArray.parseArray(list.getString("columns"));

                        isTableExist = formEntityMapper.isTableExist(list.get("id").toString());
                        if (isTableExist > 0)
                            return "该表[" + list.get("id").toString() + "]已经存在，请重新命名表名。";

                        CreateTable childrenTable = new CreateTable();
                        childrenTable.setTableName(list.get("id").toString());
                        //列对象
                        CustomTable childrenCol = new CustomTable();
                        //列对象集合
                        List<CustomTable> childrenColList = new ArrayList<>();

                        childrenCol.setChoose(true);
                        childrenCol.setCreateTableFiledName("fk_id");
                        childrenCol.setComment("主表id");
                        childrenCol.setFieldType("varchar(255)");
                        childrenColList.add(childrenCol);

                        for (int k = 0; k < childrens.size(); k++) {
                            childrenCol = new CustomTable();
                            JSONObject children = childrens.getJSONObject(k);
                            //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                            if (children.getString("compType").equals("text")
                                    || children.getString("compType").equals("barCode")
                                    || children.getString("compType").equals("alert")
                                    || children.getString("compType").equals("link")
                                    || children.getString("compType").equals("button")
                                    || children.getString("compType").equals("colorPicker")
                                    || children.getString("compType").equals("divider")
                                    || children.getString("compType").equals("upload"))
                                continue;
                            //类型如果是 input\select\checkbox、弹窗、联级选择
                            if (children.getString("compType").equals("input") ||
                                    children.getString("compType").equals("select") ||
                                    children.getString("compType").equals("checkbox")
                                    || children.getString("compType").equals("dialogList")
                                    || children.getString("compType").equals("cascader")
                                    || children.getString("compType").equals("time")) {
                                childrenCol.setChoose(true);
                                childrenCol.setCreateTableFiledName(children.getString("id"));
                                childrenCol.setComment(children.getString("label"));
                                childrenCol.setFieldType("varchar(255)");
                                childrenColList.add(childrenCol);
                            }
                            //类型如果是 大文本、富文本
                            if (children.getString("compType").equals("textarea")
                                    || children.getString("compType").equals("editor")) {
                                childrenCol.setChoose(true);
                                childrenCol.setCreateTableFiledName(children.getString("id"));
                                childrenCol.setComment(children.getString("label"));
                                childrenCol.setFieldType("text");
                                childrenColList.add(childrenCol);
                            }
                            //如果类型是 日期、时间
                            if (children.getString("compType").equals("date")) {
                                childrenCol.setChoose(true);
                                childrenCol.setCreateTableFiledName(children.getString("id"));
                                childrenCol.setComment(children.getString("label"));
                                childrenCol.setFieldType("datetime");
                                childrenColList.add(childrenCol);
                            }
                            //如果类型是 单选、评分、滑块、数字
                            if (children.getString("compType").equals("radio")
                                    || children.getString("compType").equals("rate")
                                    || children.getString("compType").equals("slider")
                                    || children.getString("compType").equals("inputNumber")) {
                                childrenCol.setChoose(true);
                                childrenCol.setCreateTableFiledName(children.getString("id"));
                                childrenCol.setComment(children.getString("label"));
                                childrenCol.setFieldType("int(11)");
                                childrenColList.add(childrenCol);
                            }
                            //如果类型是 开关
                            if (children.getString("compType").equals("Switch")) {
                                childrenCol.setChoose(true);
                                childrenCol.setCreateTableFiledName(list.getString("id"));
                                childrenCol.setComment(list.getString("label"));
                                childrenCol.setFieldType("varchar(50)");
                                childrenColList.add(customTable);
                            }
                        }
                        childrenTable.setCustomTables(childrenColList);
                        createTables.add(childrenTable);
                    }
                }
                if (list.getString("compType").equals("row")) {
                    if (!SystemConfig.IsNullOrEmpty(list.getString("columns"))) {
                        JSONArray childrens = JSONArray.parseArray(list.getString("columns"));

                        for (int k = 0; k < childrens.size(); k++) {
                            JSONObject children = childrens.getJSONObject(k);
                            JSONArray childrenList=JSONArray.parseArray(children.getString("list"));
                            for(int j = 0; j < childrenList.size(); j++){
                                JSONObject rowlist=childrenList.getJSONObject(j);
                                customTable = new CustomTable();
                                //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                                if (rowlist.getString("compType").equals("text")
                                        || rowlist.getString("compType").equals("barCode")
                                        || rowlist.getString("compType").equals("alert")
                                        || rowlist.getString("compType").equals("link")
                                        || rowlist.getString("compType").equals("button")
                                        || rowlist.getString("compType").equals("colorPicker")
                                        || rowlist.getString("compType").equals("divider")
                                        || rowlist.getString("compType").equals("upload"))
                                    continue;
                                //类型如果是 input\select\checkbox、弹窗、联级选择
                                if (rowlist.getString("compType").equals("input") ||
                                        rowlist.getString("compType").equals("select") ||
                                        rowlist.getString("compType").equals("checkbox")
                                        || rowlist.getString("compType").equals("dialogList")
                                        || rowlist.getString("compType").equals("cascader")
                                        || rowlist.getString("compType").equals("time")) {
                                    customTable.setChoose(true);
                                    customTable.setCreateTableFiledName(rowlist.getString("id"));
                                    customTable.setComment(rowlist.getString("label"));
                                    customTable.setFieldType("varchar(255)");
                                    customTableList.add(customTable);
                                }
                                if (rowlist.getString("compType").equals("dynamicTable")) {
                                    if (!SystemConfig.IsNullOrEmpty(rowlist.getString("columns"))) {
                                        JSONArray rowlistChildrens = JSONArray.parseArray(rowlist.getString("columns"));

                                        isTableExist = formEntityMapper.isTableExist(rowlist.get("id").toString());
                                        if (isTableExist > 0)
                                            return "该表[" + rowlist.get("id").toString() + "]已经存在，请重新命名表名。";

                                        CreateTable childrenTable = new CreateTable();
                                        childrenTable.setTableName(rowlist.get("id").toString());
                                        //列对象
                                        CustomTable childrenCol = new CustomTable();
                                        //列对象集合
                                        List<CustomTable> childrenColList = new ArrayList<>();

                                        childrenCol.setChoose(true);
                                        childrenCol.setCreateTableFiledName("fk_id");
                                        childrenCol.setComment("主表id");
                                        childrenCol.setFieldType("varchar(255)");
                                        childrenColList.add(childrenCol);

                                        for (int z = 0; z < rowlistChildrens.size(); z++) {
                                            childrenCol = new CustomTable();
                                            JSONObject rowlistChildren = rowlistChildrens.getJSONObject(k);
                                            //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                                            if (rowlistChildren.getString("compType").equals("text")
                                                    || rowlistChildren.getString("compType").equals("barCode")
                                                    || rowlistChildren.getString("compType").equals("alert")
                                                    || rowlistChildren.getString("compType").equals("link")
                                                    || rowlistChildren.getString("compType").equals("button")
                                                    || rowlistChildren.getString("compType").equals("colorPicker")
                                                    || rowlistChildren.getString("compType").equals("divider")
                                                    || rowlistChildren.getString("compType").equals("upload"))
                                                continue;
                                            //类型如果是 input\select\checkbox、弹窗、联级选择
                                            if (rowlistChildren.getString("compType").equals("input") ||
                                                    rowlistChildren.getString("compType").equals("select") ||
                                                    rowlistChildren.getString("compType").equals("checkbox")
                                                    || rowlistChildren.getString("compType").equals("dialogList")
                                                    || rowlistChildren.getString("compType").equals("cascader")
                                                    || rowlistChildren.getString("compType").equals("time")) {
                                                childrenCol.setChoose(true);
                                                childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                childrenCol.setComment(rowlistChildren.getString("label"));
                                                childrenCol.setFieldType("varchar(255)");
                                                childrenColList.add(childrenCol);
                                            }
                                            //类型如果是 大文本、富文本
                                            if (rowlistChildren.getString("compType").equals("textarea")
                                                    || rowlistChildren.getString("compType").equals("editor")) {
                                                childrenCol.setChoose(true);
                                                childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                childrenCol.setComment(rowlistChildren.getString("label"));
                                                childrenCol.setFieldType("text");
                                                childrenColList.add(childrenCol);
                                            }
                                            //如果类型是 日期、时间
                                            if (rowlistChildren.getString("compType").equals("date")) {
                                                childrenCol.setChoose(true);
                                                childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                childrenCol.setComment(rowlistChildren.getString("label"));
                                                childrenCol.setFieldType("datetime");
                                                childrenColList.add(childrenCol);
                                            }
                                            //如果类型是 单选、评分、滑块、数字
                                            if (rowlistChildren.getString("compType").equals("radio")
                                                    || rowlistChildren.getString("compType").equals("rate")
                                                    || rowlistChildren.getString("compType").equals("slider")
                                                    || rowlistChildren.getString("compType").equals("inputNumber")) {
                                                childrenCol.setChoose(true);
                                                childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                childrenCol.setComment(rowlistChildren.getString("label"));
                                                childrenCol.setFieldType("int(11)");
                                                childrenColList.add(childrenCol);
                                            }
                                            //如果类型是 开关
                                            if (rowlistChildren.getString("compType").equals("Switch")) {
                                                childrenCol.setChoose(true);
                                                childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                childrenCol.setComment(rowlistChildren.getString("label"));
                                                childrenCol.setFieldType("varchar(50)");
                                                childrenColList.add(customTable);
                                            }
                                        }
                                        childrenTable.setCustomTables(childrenColList);
                                        createTables.add(childrenTable);
                                    }
                                }
                                //类型如果是 大文本、富文本
                                if (rowlist.getString("compType").equals("textarea")
                                        || rowlist.getString("compType").equals("editor")) {
                                    customTable.setChoose(true);
                                    customTable.setCreateTableFiledName(rowlist.getString("id"));
                                    customTable.setComment(rowlist.getString("label"));
                                    customTable.setFieldType("text");
                                    customTableList.add(customTable);
                                }
                                //如果类型是 日期、时间
                                if (rowlist.getString("compType").equals("date")) {
                                    customTable.setChoose(true);
                                    customTable.setCreateTableFiledName(rowlist.getString("id"));
                                    customTable.setComment(rowlist.getString("label"));
                                    customTable.setFieldType("datetime");
                                    customTableList.add(customTable);
                                }
                                //如果类型是 单选、评分、滑块、数字
                                if (rowlist.getString("compType").equals("radio")
                                        || rowlist.getString("compType").equals("rate")
                                        || rowlist.getString("compType").equals("slider")
                                        || rowlist.getString("compType").equals("inputNumber")) {
                                    customTable.setChoose(true);
                                    customTable.setCreateTableFiledName(rowlist.getString("id"));
                                    customTable.setComment(rowlist.getString("label"));
                                    customTable.setFieldType("int(11)");
                                    customTableList.add(customTable);
                                }
                                //如果类型是 开关
                                if (rowlist.getString("compType").equals("Switch")) {
                                    customTable.setChoose(true);
                                    customTable.setCreateTableFiledName(list.getString("id"));
                                    customTable.setComment(list.getString("label"));
                                    customTable.setFieldType("varchar(50)");
                                    customTableList.add(customTable);
                                }
                            }
                        }
                    }
                }
                //类型如果是 input\select\checkbox、弹窗、联级选择
                if (list.getString("compType").equals("input") ||
                        list.getString("compType").equals("select") ||
                        list.getString("compType").equals("checkbox")
                        || list.getString("compType").equals("dialogList")
                        || list.getString("compType").equals("cascader")
                        || list.getString("compType").equals("time")) {
                    customTable.setChoose(true);
                    customTable.setCreateTableFiledName(list.getString("id"));
                    customTable.setComment(list.getString("label"));
                    customTable.setFieldType("varchar(255)");
                    customTableList.add(customTable);
                }
                //类型如果是 大文本、富文本
                if (list.getString("compType").equals("textarea")
                        || list.getString("compType").equals("editor")) {
                    customTable.setChoose(true);
                    customTable.setCreateTableFiledName(list.getString("id"));
                    customTable.setComment(list.getString("label"));
                    customTable.setFieldType("text");
                    customTableList.add(customTable);
                }
                //如果类型是 日期、时间
                if (list.getString("compType").equals("date")) {
                    customTable.setChoose(true);
                    customTable.setCreateTableFiledName(list.getString("id"));
                    customTable.setComment(list.getString("label"));
                    customTable.setFieldType("datetime");
                    customTableList.add(customTable);
                }
                //如果类型是 单选、评分、滑块、数字
                if (list.getString("compType").equals("radio")
                        || list.getString("compType").equals("rate")
                        || list.getString("compType").equals("slider")
                        || list.getString("compType").equals("inputNumber")) {
                    customTable.setChoose(true);
                    customTable.setCreateTableFiledName(list.getString("id"));
                    customTable.setComment(list.getString("label"));
                    customTable.setFieldType("int(11)");
                    customTableList.add(customTable);
                }
                // 如果是开关
                if (list.getString("compType").equals("Switch")) {
                    customTable.setChoose(true);
                    customTable.setCreateTableFiledName(list.getString("id"));
                    customTable.setComment(list.getString("label"));
                    customTable.setFieldType("varchar(50)");
                    customTableList.add(customTable);
                }
            }
            createTable.setCustomTables(customTableList);
            createTables.add(createTable);
            for (CreateTable cte : createTables) {
                formEntityMapper.createFormTable(cte);
            }
            msg = "suess";
            return msg;
        }
        catch (Exception ex){
            return ex.getMessage();
        }
    }
    private void AlterTableAddField(String form_data_table,String form_json) {
        //获取该表中的列
        List<String> columns = formEntityMapper.findFieldByTableName(form_data_table);
        HashMap<String, Object> hashMap = new HashMap<>();
        for (String str : columns) {
            hashMap.put(str, "");
        }

        //要创建的表的集合
        List<CreateTable> createTables = new ArrayList<>();
        if (!SystemConfig.IsNullOrEmpty(form_json)) {
            JSONObject jsonObject = JSONObject.parseObject(form_json);
            JSONObject configJson = JSONObject.parseObject(jsonObject.getString("config"));
            JSONArray listJson = JSONArray.parseArray(jsonObject.getString("list"));

            //创建表对象
            CreateTable createTable = new CreateTable();
            createTable.setTableName(configJson.get("tableName").toString());

            int changecustomTableNum=0;
            //列对象集合
            List<CustomTable> customTableList = new ArrayList<>();
            for (int i = 0; i < listJson.size(); i++) {
                //列对象
                CustomTable customTable = new CustomTable();
                JSONObject list = listJson.getJSONObject(i);
                //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                if (list.getString("compType").equals("text")
                        || list.getString("compType").equals("barCode")
                        || list.getString("compType").equals("alert")
                        || list.getString("compType").equals("link")
                        || list.getString("compType").equals("button")
                        || list.getString("compType").equals("colorPicker")
                        || list.getString("compType").equals("divider")
                        || list.getString("compType").equals("upload"))
                    continue;
                if (list.getString("compType").equals("dynamicTable")) {
                    if (!SystemConfig.IsNullOrEmpty(list.getString("columns"))) {
                        JSONArray childrens = JSONArray.parseArray(list.getString("columns"));
                        CreateTable childrenTable = new CreateTable();
                        childrenTable.setTableName(list.get("id").toString());

                        int isTableExist=formEntityMapper.isTableExist(list.get("id").toString());
                        if(isTableExist<=0) {
                            createTableByJson(list.get("id").toString(),list.getString("columns"),true);
                        }
                        else {
                            List<String> childrenColumns = formEntityMapper.findFieldByTableName(list.get("id").toString());
                            HashMap<String, Object> childrenMap = new HashMap<>();
                            for (String str : childrenColumns) {
                                childrenMap.put(str, "");
                            }
                            int childrenChangeNum=0;
                            //列对象
                            CustomTable childrenCol = new CustomTable();
                            //列对象集合
                            List<CustomTable> childrenColList = new ArrayList<>();

                            if (!childrenMap.containsKey("fk_id")) {
                                childrenChangeNum=1;
                                childrenCol.setChoose(true);
                                childrenCol.setCreateTableFiledName("fk_id");
                                childrenCol.setComment("主表id");
                                childrenCol.setFieldType("varchar(255)");
                                childrenColList.add(childrenCol);
                            }

                            for (int k = 0; k < childrens.size(); k++) {
                                childrenCol = new CustomTable();
                                JSONObject children = childrens.getJSONObject(k);
                                //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                                if (children.getString("compType").equals("text")
                                        || children.getString("compType").equals("barCode")
                                        || children.getString("compType").equals("alert")
                                        || children.getString("compType").equals("link")
                                        || children.getString("compType").equals("button")
                                        || children.getString("compType").equals("colorPicker")
                                        || children.getString("compType").equals("divider")
                                        || children.getString("compType").equals("upload"))
                                    continue;
                                if (childrenMap.containsKey(children.getString("id"))) {
                                    continue;
                                } else {
                                    //类型如果是 input\select\checkbox、弹窗、联级选择
                                    if (children.getString("compType").equals("input") ||
                                            children.getString("compType").equals("select") ||
                                            children.getString("compType").equals("checkbox")
                                            || children.getString("compType").equals("dialogList")
                                            || children.getString("compType").equals("cascader")
                                            || children.getString("compType").equals("time")) {
                                        childrenCol.setChoose(true);
                                        childrenCol.setCreateTableFiledName(children.getString("id"));
                                        childrenCol.setComment(children.getString("label"));
                                        childrenCol.setFieldType("varchar(255)");
                                        childrenColList.add(childrenCol);
                                        childrenChangeNum++;
                                    }
                                    //类型如果是 大文本、富文本
                                    if (children.getString("compType").equals("textarea")
                                            || children.getString("compType").equals("editor")) {
                                        childrenCol.setChoose(true);
                                        childrenCol.setCreateTableFiledName(children.getString("id"));
                                        childrenCol.setComment(children.getString("label"));
                                        childrenCol.setFieldType("text");
                                        childrenColList.add(childrenCol);
                                        childrenChangeNum++;
                                    }
                                    //如果类型是 日期、时间
                                    if (children.getString("compType").equals("date")) {
                                        childrenCol.setChoose(true);
                                        childrenCol.setCreateTableFiledName(children.getString("id"));
                                        childrenCol.setComment(children.getString("label"));
                                        childrenCol.setFieldType("datetime");
                                        childrenColList.add(childrenCol);
                                        childrenChangeNum++;
                                    }
                                    //如果类型是 单选、评分、滑块、数字
                                    if (children.getString("compType").equals("radio")
                                            || children.getString("compType").equals("rate")
                                            || children.getString("compType").equals("slider")
                                            || children.getString("compType").equals("inputNumber")) {
                                        childrenCol.setChoose(true);
                                        childrenCol.setCreateTableFiledName(children.getString("id"));
                                        childrenCol.setComment(children.getString("label"));
                                        childrenCol.setFieldType("int(11)");
                                        childrenColList.add(childrenCol);
                                        childrenChangeNum++;
                                    }
                                    //如果类型是 开关
                                    if (children.getString("compType").equals("Switch")) {
                                        childrenCol.setChoose(true);
                                        childrenCol.setCreateTableFiledName(children.getString("id"));
                                        childrenCol.setComment(children.getString("label"));
                                        childrenCol.setFieldType("varchar(50)");
                                        childrenColList.add(childrenCol);
                                        childrenChangeNum++;
                                    }
                                }
                            }
                            if(childrenChangeNum>0) {
                                childrenTable.setCustomTables(childrenColList);
                                createTables.add(childrenTable);
                            }
                        }
                    }
                }
                if (list.getString("compType").equals("row")) {
                    if (!SystemConfig.IsNullOrEmpty(list.getString("columns"))) {
                        JSONArray childrens = JSONArray.parseArray(list.getString("columns"));
                        for (int k = 0; k < childrens.size(); k++) {
                            JSONObject children = childrens.getJSONObject(k);
                            JSONArray childrenList=JSONArray.parseArray(children.getString("list"));
                            for(int j = 0; j < childrenList.size(); j++){
                                JSONObject rowlist=childrenList.getJSONObject(j);
                                customTable = new CustomTable();
                                //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                                if (rowlist.getString("compType").equals("text")
                                        || rowlist.getString("compType").equals("barCode")
                                        || rowlist.getString("compType").equals("alert")
                                        || rowlist.getString("compType").equals("link")
                                        || rowlist.getString("compType").equals("button")
                                        || rowlist.getString("compType").equals("colorPicker")
                                        || rowlist.getString("compType").equals("divider")
                                        || rowlist.getString("compType").equals("upload"))
                                    continue;
                                if (hashMap.containsKey(rowlist.getString("id"))) {
                                    continue;
                                } else {
                                    //类型如果是 input\select\checkbox、弹窗、联级选择
                                    if (rowlist.getString("compType").equals("input") ||
                                            rowlist.getString("compType").equals("select") ||
                                            rowlist.getString("compType").equals("checkbox")
                                            || rowlist.getString("compType").equals("dialogList")
                                            || rowlist.getString("compType").equals("cascader")
                                            || rowlist.getString("compType").equals("time")) {
                                        customTable.setChoose(true);
                                        customTable.setCreateTableFiledName(rowlist.getString("id"));
                                        customTable.setComment(rowlist.getString("label"));
                                        customTable.setFieldType("varchar(255)");
                                        customTableList.add(customTable);
                                        changecustomTableNum++;
                                    }
                                    if (rowlist.getString("compType").equals("dynamicTable")) {
                                        if (!SystemConfig.IsNullOrEmpty(rowlist.getString("columns"))) {
                                            JSONArray rowlistChildrens = JSONArray.parseArray(rowlist.getString("columns"));
                                            CreateTable childrenTable = new CreateTable();
                                            childrenTable.setTableName(rowlist.get("id").toString());

                                            int isTableExist=formEntityMapper.isTableExist(rowlist.get("id").toString());
                                            if(isTableExist<=0) {
                                                createTableByJson(rowlist.get("id").toString(),rowlist.getString("columns"),true);
                                            }
                                            else {
                                                List<String> childrenColumns = formEntityMapper.findFieldByTableName(rowlist.get("id").toString());
                                                HashMap<String, Object> childrenMap = new HashMap<>();
                                                for (String str : childrenColumns) {
                                                    childrenMap.put(str, "");
                                                }
                                                int childrenChangeNum=0;
                                                //列对象
                                                CustomTable childrenCol = new CustomTable();
                                                //列对象集合
                                                List<CustomTable> childrenColList = new ArrayList<>();

                                                if (!childrenMap.containsKey("fk_id")) {
                                                    childrenChangeNum=1;
                                                    childrenCol.setChoose(true);
                                                    childrenCol.setCreateTableFiledName("fk_id");
                                                    childrenCol.setComment("主表id");
                                                    childrenCol.setFieldType("varchar(255)");
                                                    childrenColList.add(childrenCol);
                                                }

                                                for (int z = 0; z < rowlistChildrens.size(); z++) {
                                                    childrenCol = new CustomTable();
                                                    JSONObject rowlistChildren = rowlistChildrens.getJSONObject(z);
                                                    //如果类型是文本、二维码、alert、超链接、按钮、颜色选择器、分割线、附件
                                                    if (rowlistChildren.getString("compType").equals("text")
                                                            || rowlistChildren.getString("compType").equals("barCode")
                                                            || rowlistChildren.getString("compType").equals("alert")
                                                            || rowlistChildren.getString("compType").equals("link")
                                                            || rowlistChildren.getString("compType").equals("button")
                                                            || rowlistChildren.getString("compType").equals("colorPicker")
                                                            || rowlistChildren.getString("compType").equals("divider")
                                                            || rowlistChildren.getString("compType").equals("upload"))
                                                        continue;
                                                    if (childrenMap.containsKey(rowlistChildren.getString("id"))) {
                                                        continue;
                                                    } else {
                                                        //类型如果是 input\select\checkbox、弹窗、联级选择
                                                        if (rowlistChildren.getString("compType").equals("input") ||
                                                                rowlistChildren.getString("compType").equals("select") ||
                                                                rowlistChildren.getString("compType").equals("checkbox")
                                                                || rowlistChildren.getString("compType").equals("dialogList")
                                                                || rowlistChildren.getString("compType").equals("cascader")
                                                                || rowlistChildren.getString("compType").equals("time")) {
                                                            childrenCol.setChoose(true);
                                                            childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                            childrenCol.setComment(rowlistChildren.getString("label"));
                                                            childrenCol.setFieldType("varchar(255)");
                                                            childrenColList.add(childrenCol);
                                                            childrenChangeNum++;
                                                        }
                                                        //类型如果是 大文本、富文本
                                                        if (rowlistChildren.getString("compType").equals("textarea")
                                                                || rowlistChildren.getString("compType").equals("editor")) {
                                                            childrenCol.setChoose(true);
                                                            childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                            childrenCol.setComment(rowlistChildren.getString("label"));
                                                            childrenCol.setFieldType("text");
                                                            childrenColList.add(childrenCol);
                                                            childrenChangeNum++;
                                                        }
                                                        //如果类型是 日期、时间
                                                        if (rowlistChildren.getString("compType").equals("date")) {
                                                            childrenCol.setChoose(true);
                                                            childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                            childrenCol.setComment(rowlistChildren.getString("label"));
                                                            childrenCol.setFieldType("datetime");
                                                            childrenColList.add(childrenCol);
                                                            childrenChangeNum++;
                                                        }
                                                        //如果类型是 单选、评分、滑块、数字
                                                        if (rowlistChildren.getString("compType").equals("radio")
                                                                || rowlistChildren.getString("compType").equals("rate")
                                                                || rowlistChildren.getString("compType").equals("slider")
                                                                || rowlistChildren.getString("compType").equals("inputNumber")) {
                                                            childrenCol.setChoose(true);
                                                            childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                            childrenCol.setComment(rowlistChildren.getString("label"));
                                                            childrenCol.setFieldType("int(11)");
                                                            childrenColList.add(childrenCol);
                                                            childrenChangeNum++;
                                                        }
                                                        //如果类型是 开关
                                                        if (rowlistChildren.getString("compType").equals("Switch")) {
                                                            childrenCol.setChoose(true);
                                                            childrenCol.setCreateTableFiledName(rowlistChildren.getString("id"));
                                                            childrenCol.setComment(rowlistChildren.getString("label"));
                                                            childrenCol.setFieldType("varchar(50)");
                                                            childrenColList.add(customTable);
                                                            childrenChangeNum++;
                                                        }
                                                    }
                                                }
                                                if(childrenChangeNum>0) {
                                                    childrenTable.setCustomTables(childrenColList);
                                                    createTables.add(childrenTable);
                                                }
                                            }
                                        }
                                    }
                                    //类型如果是 大文本、富文本
                                    if (rowlist.getString("compType").equals("textarea")
                                            || rowlist.getString("compType").equals("editor")) {
                                        customTable.setChoose(true);
                                        customTable.setCreateTableFiledName(rowlist.getString("id"));
                                        customTable.setComment(rowlist.getString("label"));
                                        customTable.setFieldType("text");
                                        customTableList.add(customTable);
                                        changecustomTableNum++;
                                    }
                                    //如果类型是 日期、时间
                                    if (rowlist.getString("compType").equals("date")) {
                                        customTable.setChoose(true);
                                        customTable.setCreateTableFiledName(rowlist.getString("id"));
                                        customTable.setComment(rowlist.getString("label"));
                                        customTable.setFieldType("datetime");
                                        customTableList.add(customTable);
                                        changecustomTableNum++;
                                    }
                                    //如果类型是 单选、评分、滑块、数字
                                    if (rowlist.getString("compType").equals("radio")
                                            || rowlist.getString("compType").equals("rate")
                                            || rowlist.getString("compType").equals("slider")
                                            || rowlist.getString("compType").equals("inputNumber")) {
                                        customTable.setChoose(true);
                                        customTable.setCreateTableFiledName(rowlist.getString("id"));
                                        customTable.setComment(rowlist.getString("label"));
                                        customTable.setFieldType("int(11)");
                                        customTableList.add(customTable);
                                        changecustomTableNum++;
                                    }
                                    //如果类型是 开关
                                    if (rowlist.getString("compType").equals("Switch")) {
                                        customTable.setChoose(true);
                                        customTable.setCreateTableFiledName(rowlist.getString("id"));
                                        customTable.setComment(rowlist.getString("label"));
                                        customTable.setFieldType("varchar(50)");
                                        customTableList.add(customTable);
                                        changecustomTableNum++;
                                    }
                                }
                            }

                        }
                    }
                }
                if (hashMap.containsKey(list.getString("id"))) {
                    continue;
                } else {
                    //类型如果是 input\select\checkbox、弹窗、联级选择
                    if (list.getString("compType").equals("input") ||
                            list.getString("compType").equals("select") ||
                            list.getString("compType").equals("checkbox")
                            || list.getString("compType").equals("dialogList")
                            || list.getString("compType").equals("cascader")
                            || list.getString("compType").equals("time")) {
                        customTable.setChoose(true);
                        customTable.setCreateTableFiledName(list.getString("id"));
                        customTable.setComment(list.getString("label"));
                        customTable.setFieldType("varchar(255)");
                        customTableList.add(customTable);
                        changecustomTableNum++;
                    }
                    //类型如果是 大文本、富文本
                    if (list.getString("compType").equals("textarea")
                            || list.getString("compType").equals("editor")) {
                        customTable.setChoose(true);
                        customTable.setCreateTableFiledName(list.getString("id"));
                        customTable.setComment(list.getString("label"));
                        customTable.setFieldType("text");
                        customTableList.add(customTable);
                        changecustomTableNum++;
                    }
                    //如果类型是 日期、时间
                    if (list.getString("compType").equals("date")) {
                        customTable.setChoose(true);
                        customTable.setCreateTableFiledName(list.getString("id"));
                        customTable.setComment(list.getString("label"));
                        customTable.setFieldType("datetime");
                        customTableList.add(customTable);
                        changecustomTableNum++;
                    }
                    //如果类型是 单选、评分、滑块、数字
                    if (list.getString("compType").equals("radio")
                            || list.getString("compType").equals("rate")
                            || list.getString("compType").equals("slider")
                            || list.getString("compType").equals("inputNumber")) {
                        customTable.setChoose(true);
                        customTable.setCreateTableFiledName(list.getString("id"));
                        customTable.setComment(list.getString("label"));
                        customTable.setFieldType("int(11)");
                        customTableList.add(customTable);
                        changecustomTableNum++;
                    }
                    // 如果是开关
                    if (list.getString("compType").equals("Switch")) {
                        customTable.setChoose(true);
                        customTable.setCreateTableFiledName(list.getString("id"));
                        customTable.setComment(list.getString("label"));
                        customTable.setFieldType("varchar(50)");
                        customTableList.add(customTable);
                        changecustomTableNum++;
                    }
                }
            }
            if(changecustomTableNum>0) {
                createTable.setCustomTables(customTableList);
                createTables.add(createTable);
            }
        }
        if(createTables.size()>0) {
            for (CreateTable cte : createTables) {
                int isTableExist=formEntityMapper.isTableExist(cte.getTableName());
                if(isTableExist>0) {
                    formEntityMapper.AlterTableAddField(cte);
                }
                else{
                    formEntityMapper.createFormTable(cte);
                }
            }
        }
    }
    @Override
    public String saveFormDataJson(String id,String mapJson,String dataJson){
        String guid=id;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username=authentication.getName();
        try{
            GenerWork generWork=new GenerWork();
            if(!SystemConfig.IsNullOrEmpty(guid)){
                generWork=generWorkMapper.generWork(guid);
                generWork.setId(guid);
                generWork.setMap_json(mapJson);
                generWork.setData_json(dataJson);
                generWorkMapper.updateGenerWork(generWork);
            }
            else {
                guid=UUID.randomUUID().toString();
                generWork.setId(guid);
                generWork.setMap_json(mapJson);
                generWork.setData_json(dataJson);
                generWork.setStarter(username);
                generWork.setCreatetime(DateTime.now().toString("yyyy-MM-dd hh:mm:ss"));
                generWorkMapper.insertGenerWork(generWork);
            }
            return guid;
        }
        catch (Exception ex){
            return "error";
        }
    }
    @Override
    public HashMap<String,Object> getFromDataList(Integer pageNum, Integer pagesize, String xmmc){
        //获取当前登录用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username=authentication.getName();
        List<GenerWork> generWorkList=null;
        if(pagesize==0){
            generWorkList=generWorkMapper.allGenerWork(username,xmmc);
        }
        else {
            generWorkList = generWorkMapper.generWorkList(username, pageNum, pagesize, xmmc);
        }
        List<GenerWork> generWorkNum=generWorkMapper.generWorkNum(username,xmmc);
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("list",generWorkList);
        hashMap.put("total",generWorkNum.size());
        return hashMap;
    }
    @Override
    public HashMap<String,Object> getTodoList(Integer pageNum,Integer pagesize,String flowName){
        //获取当前登录用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username=authentication.getName();
        List<GenerWork> generWorkList=generWorkMapper.getTodoList(username,pageNum,pagesize,flowName);
        List<GenerWork> generWorkNum=generWorkMapper.getTodoListNum(username,flowName);
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("list",generWorkList);
        hashMap.put("total",generWorkNum.size());
        return hashMap;
    }
    @Override
    public GenerWork getGenerWork(String id){
        GenerWork generWork=generWorkMapper.generWork(id);
        return generWork;
    }
    @Override
    public String deleteGenerWork(String id){
        String msg="";
        try{
            generWorkMapper.deleteGenerWork(id);
            msg="suess";
            return msg;
        }
        catch (Exception ex){
            return ex.getMessage();
        }
    }
}
